package com.cz.ssyx.search.controller;

import com.cz.ssyx.common.result.Result;
import com.cz.ssyx.model.search.SkuEs;
import com.cz.ssyx.search.service.SkuService;
import com.cz.ssyx.vo.search.SkuEsQueryVo;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @descriptions:
 * @author: Joker.
 * @date: 2024/4/19 20:40
 * @version: 1.0
 */

@RestController
@RequestMapping("/api/search/sku")
public class SkuApiController {

    @Autowired
    private SkuService skuService;

    @GetMapping("/autocomplete/{prefix}")
    public List<String> autocompleteSkuName(@PathVariable String prefix) {
        return skuService.autocompleteSkuName(prefix);
    }

    //获取爆款商品
    @GetMapping("/inner/findHotSkuList")
    public List<SkuEs> findHotSkuList() {
        return skuService.findHotSkuList();
    }

    //删除商品
    @DeleteMapping("/inner/deleteSku/{skuId}")
    public Result deleteSku(@PathVariable Long skuId) {
        skuService.deleteSku(skuId);
        return Result.ok(skuId);
    }

    //上架
    @GetMapping("/inner/upperSku/{skuId}")
    public Result upperSku(@PathVariable Long skuId) {
        skuService.upperSku(skuId);
        return Result.ok(null);
    }

    //下架
    @GetMapping("/inner/lowerSku/{skuId}")
    public Result lowerSku(@PathVariable Long skuId) {
        skuService.lowerSku(skuId);
        return Result.ok(null);
    }

    //根据分类查询商品
    @GetMapping("{page}/{limit}")
    public Result listSku(@PathVariable Integer page,
                          @PathVariable Integer limit,
                          SkuEsQueryVo skuEsQueryVo) {

        //创建pageable对象，0代表第一页
        Pageable pageable = PageRequest.of(page-1, limit);
        Page<SkuEs> skuEsPage = skuService.search(pageable, skuEsQueryVo);
        return Result.ok(skuEsPage);
    }

    //更新商品热度
    @GetMapping("inner/incrHotScore/{skuId}")
    public Boolean incrHotScore(@PathVariable Long skuId) {
        skuService.incrHotScore(skuId);
        return true;
    }
}
